/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#pragma once

#include "../core/server.h"
#include "room.h"
#include "defs.h"
#include "clients.h"

namespace greenhills {

namespace net {
    class CMsgServerSocketConnection;
}

namespace io {
    class CConnection;
}

namespace server {

class CClient;

/**
  * @brief Greenhills server implementation
  *
  * Receives requests from clients and sends notifications to clients when
  * needed.
  */
class CServer : public greenhills::core::CServer,
    public greenhills::core::IRoomListener
{

public:
    CServer();
    virtual ~CServer();

public: //core::IServer
    virtual greenhills::core::CClientId login(const std::string &pUsername,
            const std::string &pPassword);
    virtual bool logout(const greenhills::core::CClientId &pClientId);

    virtual greenhills::core::CRoomId createRoom(
            const greenhills::core::CClientId &pClientId,
            const std::string &pRoomName,
            const greenhills::core::CRoomType &pRoomType,
            const std::string &pPassword);
    virtual bool joinRoom(const greenhills::core::CClientId &pClientId,
            const greenhills::core::CRoomId &pRoomId,
            const std::string &pPassword);
    virtual bool leaveRoom(const greenhills::core::CClientId &pClientId,
            const greenhills::core::CRoomId &pRoomId);

    virtual greenhills::core::CItemId addItem(
            const greenhills::core::CClientId &pClientId,
            const greenhills::core::CRoomId &pRoomId,
            const std::string &pItemName,
            const greenhills::core::CItemType &pItemType);
    virtual bool removeItem(const greenhills::core::CClientId &pClientId,
            const greenhills::core::CRoomId &pRoomId,
            const greenhills::core::CItemId &pItemId);
    virtual bool updateItem(const greenhills::core::CClientId &pClientId,
            const greenhills::core::CRoomId &pRoomId,
            const greenhills::core::CItemId &pItemId,
            const unsigned char *pData, unsigned int pLength);

public: //core::IRoomListener
    virtual void aboutToAddItem(greenhills::core::IRoom *pRoom,
        const greenhills::core::CItemId &pItemId);
    virtual void aboutToUpdateItem(greenhills::core::IRoom *pRoom,
        const greenhills::core::CItemId &pItemId);
    virtual void aboutToRemoveItem(greenhills::core::IRoom *pRoom,
        const greenhills::core::CItemId &pItemId);

    virtual void itemAdded(greenhills::core::IRoom *pRoom,
            const greenhills::core::CItemId &pItemId,
            const std::string &pItemName,
            const greenhills::core::CItemType &pItemType);
    virtual void itemUpdated(greenhills::core::IRoom *pRoom,
            const greenhills::core::CItemId &pItemId,
            const unsigned char *pData, unsigned int pLength);
    virtual void itemRemoved(greenhills::core::IRoom *pRoom,
            const greenhills::core::CItemId &pItemId);

public:
    CClients &clients() {return mClients;}
    greenhills::server::CRoom *room(const greenhills::core::CRoomId &roomId);

private:
    static greenhills::core::CItemId getUniqueItemId();

private:
    server::CRoom *mDefaultRoom;
    CClients mClients;
};

} //namespace server
} //namespace greenhills
